import json
import math
import requests
def get_light_rect(sign_info_str: str) -> list:
    """
    解析红绿灯区域坐标（id=1）
    输入：左上、右下两点坐标
    返回格式：[左上x,左上y, 右上x,右上y, 右下x,右下y, 左下x,左下y]
    """
    try:
        sign_info = json.loads(sign_info_str)
        for item in sign_info:
            if item['id'] == '1':
                points = item['data']
                if len(points) != 2:
                    print("红绿灯坐标需要2个点")
                    return []
                
                # 提取并验证坐标点
                left_top = points[0]
                right_bottom = points[1]
                
                # 生成完整四点坐标
                return [
                    left_top[0], left_top[1],          # 左上
                    right_bottom[0], right_bottom[1]
                ]
        return []
    except Exception as e:
        print(f"红绿灯坐标解析失败: {str(e)}")
        return []

def get_pts_blue(sign_info_str: str) -> list:
    """
    解析人行道区域坐标（id=2）
    返回格式：最小外接矩形坐标 [x1,y1, x2,y2, x3,y3, x4,y4]
    """
    try:
        sign_info = json.loads(sign_info_str)
        for item in sign_info:
            if item['id'] == '2':
                polygon = item['data']
                if len(polygon) < 3:
                    print("人行道需要至少3个点")
                    return []
                
                # 计算最小外接矩形
                x_coords = [p[0] for p in polygon]
                y_coords = [p[1] for p in polygon]
                
                min_x, max_x = min(x_coords), max(x_coords)
                min_y, max_y = min(y_coords), max(y_coords)
                
                return [
                    min_x, min_y,  # 左上
                    max_x, min_y,  # 右上
                    max_x, max_y,  # 右下
                    min_x, max_y   # 左下
                ]
        return []
    except Exception as e:
        print(f"人行道坐标解析失败: {str(e)}")
        return []
    

# 正常请求
def get_camera_info(name):
    # url = "http://192.168.0.186:9688/shibie/aiApi/getCameraInfo"
    url = "http://192.168.0.142:80/aiApi/getCameraInfo"
    params = {"cameraName": name}
    
    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        # print(response.json())
        return response.json()
    except requests.exceptions.HTTPError as err:
        print(f"HTTP错误: {err}")
    except Exception as err:
        print(f"其他错误: {err}")
        
def get_rect_info(name):
    light_rect = get_light_rect(get_camera_info(name)['result']['signInfo']) 
    parr_rect = get_pts_blue(get_camera_info(name)['result']['signInfo'])
    return [light_rect, parr_rect]
# 测试用例
# if __name__ == "__main__":
#     # 正常情况
#     print(get_rect_info("高新区xx摄像头")[0])
#     print(get_rect_info("高新区xx摄像头")[1])

    # print("正常响应：", get_camera_info("高新区xx摄像头"))
    # print(  get_light_rect(get_camera_info("高新区xx摄像头")['result']['signInfo']) )
    # print(  get_pts_blue(get_camera_info("高新区xx摄像头")['result']['signInfo']) )
    # 不存在的摄像头
    # print("不存在测试：", get_camera_info("不存在摄像头"))
    
    # 缺少参数测试
    bad_response = requests